#!/usr/bin/node

const log = console.log;

let buf1 = new Buffer(256);
buf1[0] = 0;
log(`buf1 length: ${buf1.length}`);
log('buf1:\n', buf1);

//循环来初始化buffer中的每个字节
for(let i=0; i<buf1.length; i++){
  buf1[i] = i;
}
log('buf1:\n', buf1);

//切片
let buf2 = buf1.slice(246,256);
log('buf2:',buf2);
log(`buf2 length: ${buf2.length}\n`);

//填充操作
buf2.fill(0);
log('buf2',buf2);
log('buf2\n',buf2.toJSON());

//用数组初始化
let arr = ['a',0xba,0xdf,0,255,10];
let buf3 = new Buffer(arr);
log('buf3:',buf3);
log(`buf3 length: ${buf3.length}\n`);

//用字符串初始化
let str = 'hello world';
let buf4 = new Buffer(str);
log('buf4:',buf4);
log(`buf4 length: ${buf4.length}`);
log(`buf4 string: ${buf4.toString('utf-8')}\n`);

//复制
buf4.copy(buf3,0,0,buf3.length);
log('buf3:',buf3);
log(`buf3 length: ${buf3.length}`);
log(`buf3 string: ${buf3.toString('utf-8')}`);

